'use strict';
const {
    Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
    class Permission extends Model {
        /**
         * Helper method for defining associations.
         * This method is not a part of Sequelize lifecycle.
         * The `models/index` file will call this method automatically.
         */
        static associate(models) {
            // define association here
            models.Permission.belongsToMany(models.Role, {
                as: 'roles',
                through: 'RolePermission',
                foreignKey: 'permissionId'
            });
            models.Permission.hasMany(models.Permission, {as: 'children', foreignKey: 'parentId'})
        }
    };
    Permission.init({
        parentId: DataTypes.INTEGER,
        level: DataTypes.INTEGER,
        title: DataTypes.STRING,
        name: DataTypes.STRING,
        path: DataTypes.STRING,
        component: DataTypes.STRING,
        icon: DataTypes.STRING,
        sort: DataTypes.INTEGER,
        router: DataTypes.BOOLEAN,
        method: DataTypes.STRING,
        api: DataTypes.STRING
    }, {
        sequelize,
        modelName: 'Permission',
    });
    return Permission;
};
